package com.shundr.truck.view;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.actionbarsherlock.view.Window;
import com.shundr.R;
import com.shundr.base.BaseDialog;
import com.shundr.common.view.CListView;
import com.shundr.truck.model.TruckInfo;

public class DialogChoiceTruck extends BaseDialog {
	
	private Context mContext;
	private CListView mListView;
	private TruckAdapter mAdapter;
	private List<TruckInfo> mList = new ArrayList<TruckInfo>();
	
	private TruckChoiceListener itemClickListener;
	
	public DialogChoiceTruck(Context context,List<TruckInfo> list,TruckChoiceListener listener){
		this.mContext = context;
		this.mList = list;
		this.itemClickListener = listener;
	}
	
	@SuppressLint("InflateParams")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		getDialog().requestWindowFeature((int) Window.FEATURE_NO_TITLE);
		setStyle(STYLE_NO_TITLE, R.style.dialog_no_overlay);
		View view = inflater.inflate(R.layout.dialog_order_status, null);
		mListView = (CListView) view.findViewById(R.id.lv_list);
		mAdapter = new TruckAdapter(mList, mContext);
		mListView.setAdapter(mAdapter);
		
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				try {
					if(itemClickListener == null)
						return;
					itemClickListener.onItemClick(mList.get(position - 1));
				} catch (Exception e) {
					e.printStackTrace();
				}
				dismiss();
			}
		});
		return view;
	}
	
	public class TruckAdapter extends BaseAdapter{
		public List<TruckInfo> mList;
		private Context mContext;

		@Override
		public int getCount() {
			return mList.size();
		}

		@Override
		public Object getItem(int index) {
			return mList.get(index);
		}

		@Override
		public long getItemId(int id) {
			return id;
		}

		public TruckAdapter(List<TruckInfo> list, Context mContext) {
			super();
			this.mList = list;
			this.mContext = mContext;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup viewGroup) {
			ViewHold viewHold = null;
			if(convertView == null){
				convertView = LayoutInflater.from(mContext).inflate(R.layout.item_order_status,null);
				viewHold = new ViewHold();
				viewHold.tvValue = (TextView) convertView.findViewById(R.id.tv_value);
				convertView.setTag(viewHold);
			}else{
				viewHold = (ViewHold) convertView.getTag();
			}
			try {
				TruckInfo info = mList.get(position);
				viewHold.tvValue.setText(info.getTruckPlateNumber());
			} catch (Exception e) {
				e.printStackTrace();
			}
			return convertView;
		}

		class ViewHold{
			TextView tvValue;
		}
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		return super.onCreateDialog(savedInstanceState);
	}
	
	public interface TruckChoiceListener{
		public void onItemClick(TruckInfo info);
	}

	
}
